//
//  MyInfoViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/17.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "MyInfoViewController.h"
#import "MyInfoTableViewCell.h"
#import "ZBPhotoTaker.h"
#import "MySexViewController.h"
#import "MyNickNameViewController.h"
#import "MySignViewController.h"

static NSString * const MY_INFO_CELL = @"myInfoCell";

@interface MyInfoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *myInfoTV;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSMutableDictionary *userInfo;
@property (nonatomic, strong) ZBPhotoTaker *photoTaker;

@end

@implementation MyInfoViewController

- (void) viewWillAppear:(BOOL)animated {
    self.userInfo = [[TMCache sharedCache] objectForKey:@"userInfo"];
    [self.myInfoTV reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setStyle];
}

- (void) setStyle {
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    self.navigationItem.title = @"编辑资料";
    self.view.backgroundColor = GRAY_COLOR;
    
    self.myInfoTV = [UITableView newAutoLayoutView];
    [self.view addSubview:_myInfoTV];
    
    [_myInfoTV autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [_myInfoTV autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_myInfoTV autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    [_myInfoTV autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    
    _myInfoTV.delegate = self;
    _myInfoTV.dataSource = self;
    _myInfoTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myInfoTV.backgroundColor = GRAY_COLOR;
    
    [_myInfoTV registerClass:[MyInfoTableViewCell class] forCellReuseIdentifier:MY_INFO_CELL];

    self.titleArr = @[@[@"头像"],@[@"昵称", @"性别"], @[@"个性签名"]];
    
    self.userInfo = [[TMCache sharedCache] objectForKey:@"userInfo"];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 2;
    }else{
        return 1;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else{
        return 10;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 50;
    }else{
        return 90;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MY_INFO_CELL forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0) {
        cell.titleLB.text = _titleArr[indexPath.section][indexPath.row];
        cell.titleLB.hidden = NO;
        cell.badgeLB.hidden = YES;
        cell.avatarImg.hidden = NO;
        cell.signBakLB.hidden = YES;
        cell.signLB.hidden = YES;
        
        NSURL *imageUrl = [NSURL URLWithString:[_userInfo objectForKey:@"avatar"]];
        [cell.avatarImg sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"defaultAvatar"]];
        
    }else if (indexPath.section == 1){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.titleLB.text = _titleArr[indexPath.section][indexPath.row];
        cell.titleLB.hidden = NO;
        cell.badgeLB.hidden = NO;
        cell.avatarImg.hidden = YES;
        cell.signBakLB.hidden = YES;
        cell.signLB.hidden = YES;
        if (indexPath.row == 0) {
            if (![[_userInfo objectForKey:@"nickname"] isEqualToString:@""]) {
                cell.badgeLB.text = [_userInfo objectForKey:@"nickname"];
            }
        }else{
            if (![[_userInfo objectForKey:@"sex_name"] isEqualToString:@""]) {
                cell.badgeLB.text = [_userInfo objectForKey:@"sex_name"];
            }
        }

    
    }else{
        cell.titleLB.hidden = NO;
        cell.badgeLB.hidden = NO;
        cell.avatarImg.hidden = YES;
        cell.signBakLB.hidden = NO;
        cell.signLB.hidden = NO;
        cell.signLB.text = _titleArr[indexPath.section][indexPath.row];
        
        cell.signBakLB.text = [_userInfo objectForKey:@"sign"];
    }
    
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        //编辑头像
        [self uploadAvatar];
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            // 编辑昵称
            MyNickNameViewController *nickNameVC = [[MyNickNameViewController alloc] init];
            [self.navigationController pushViewController:nickNameVC animated:YES];
        }else{
            // 编辑性别
            MySexViewController *sexVC = [[MySexViewController alloc] init];
            [self.navigationController pushViewController:sexVC animated:YES];
        }
    }else{
        // 编辑个性签名
        MySignViewController *signVC =[[MySignViewController alloc] init];
        [self.navigationController pushViewController:signVC animated:YES];
    }
}

#pragma mark - 更新头像
- (void) uploadAvatar {
    self.photoTaker = Nil;
    self.photoTaker = [[ZBPhotoTaker alloc] initWithViewController:self];
    
    [_photoTaker takePhotoFrom:ZBPhotoTakerSourceFromGallery allowsEditing:YES finished:^(UIImage *image) {
        [SVProgressHUD show];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setObject:[NSString stringWithFormat:@"%@", [[TMCache sharedCache] objectForKey:@"SSID"]] forKey:@"ssid"];
        [parameters setObject:@"avatar" forKey:@"type"];
        
        [[HttpManager instance] uploadFile:@"Util/uploadFile" imageData:image parameters:parameters success:^(NSDictionary *result) {
            
            NSString *alertTmp = [[result objectForKey:@"data"] objectForKey:@"info"];
            NSDictionary *userInfoTmp = [result objectForKey:@"data"];
            
            [[TMCache sharedCache] setObject:userInfoTmp forKey:@"userInfo"];

            _userInfo = [[TMCache sharedCache] objectForKey:@"userInfo"];
            
            [_myInfoTV reloadData];
            
            [SVProgressHUD showInfoWithStatus:alertTmp maskType:SVProgressHUDMaskTypeBlack];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            [SVProgressHUD showErrorWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
            
        }];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
